package com.acme.bankapp.domine.account;

import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {
    private double overdraft;

    public double getOverdraft() {
        return overdraft;
    }

    public CheckingAccount(final double amount,
            final double overdraft) throws IllegalArgumentException {
        super(amount);
        setType("CheckingAccount");
        if (overdraft < 0) {
            throw new IllegalArgumentException("CheckingAccount: "
                    + "Overdraft can not be negative");
        }
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(final double amount)
            throws OverDraftLimitExceededException {
        if (getBalance() + overdraft >= amount) {
            setBalance(getBalance() - amount);
        } else {
            throw new OverDraftLimitExceededException("CheckingAccount: "
                    + "Amount withdraw can not be negative");
        }
    }

    @Override
    public double maximumAmountToWithdraw() {
        return getBalance() + overdraft;
    }

    @Override
    public String toString() {
        return "CheckingAccount{balance=" + getBalance()
                + "; overdraft=" + getOverdraft() + "}";
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CheckingAccount that = (CheckingAccount) o;

        if (this.getId() != that.getId()) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        long temp = 31 * super.hashCode() + overdraft != +0.0d
                ? Double.doubleToLongBits(overdraft) : 0L;
        return (int) (temp ^ (temp >>> 32));
    }

    public AccountType getAccountType() {
        return  AccountType.CHECKING;
    }
}
